package com.apadog.study.utils;



import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
import org.apache.hc.core5.util.Timeout;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.client5.http.config.RequestConfig;


public class RestTemplateUtil {

    private RestTemplateUtil(){}

    private static class Build{
        private static final RestTemplate INSTANCE = get();

        private static final RestTemplate INSTANCE_PROXY = getProxy();

        private static RestTemplate getProxy(){
            //Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("8.155.6.74", 20005)); // 设置代理服务器地址和端口

            // 配置代理信息
            HttpHost proxy = new HttpHost("47.111.118.133", 20005); // 设置代理地址和端口

            // 配置请求的超时时间等参数
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectionRequestTimeout(Timeout.of(6, TimeUnit.SECONDS)) // 设置连接超时
                    .setResponseTimeout(Timeout.of(6, TimeUnit.SECONDS)) // 设置响应超时
                    .build();

            // 添加拦截器，设置默认 Accept 头
            ClientHttpRequestInterceptor acceptHeaderInterceptor = (request, body, execution) -> {
                HttpHeaders headers = request.getHeaders();
                headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
                return execution.execute(request, body);
            };

            // 创建 HttpClient 实例，使用代理配置
            CloseableHttpClient httpClient = HttpClients.custom()
                    .setDefaultRequestConfig(requestConfig)
                    .setRoutePlanner(new DefaultProxyRoutePlanner(proxy)) // 使用代理路由
                    .build();

            // 使用 HttpClient 创建 HttpRequestFactory
            HttpComponentsClientHttpRequestFactory factory =
                    new HttpComponentsClientHttpRequestFactory(httpClient);
            RestTemplate restTemplate = new RestTemplate(factory);

            List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
            interceptors.add(acceptHeaderInterceptor);
            restTemplate.setInterceptors(interceptors);

            List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

            // 创建并添加 Jackson JSON 消息转换器
            MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
            messageConverters.add(jsonConverter);
            messageConverters.add(new FastJsonHttpMessageConverter());
            messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));


            restTemplate.setMessageConverters(messageConverters);
            return restTemplate;
        }

        private static RestTemplate get(){

            // 配置请求的超时时间等参数
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectionRequestTimeout(Timeout.of(6, TimeUnit.SECONDS)) // 设置连接超时
                    .setResponseTimeout(Timeout.of(6, TimeUnit.SECONDS)) // 设置响应超时
                    .build();

            // 添加拦截器，设置默认 Accept 头
            ClientHttpRequestInterceptor acceptHeaderInterceptor = (request, body, execution) -> {
                HttpHeaders headers = request.getHeaders();
                headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
                return execution.execute(request, body);
            };

            // 创建 HttpClient 实例，使用代理配置
            CloseableHttpClient httpClient = HttpClients.custom()
                    .setDefaultRequestConfig(requestConfig)
                    .build();

            // 使用 HttpClient 创建 HttpRequestFactory
            HttpComponentsClientHttpRequestFactory factory =
                    new HttpComponentsClientHttpRequestFactory(httpClient);
            RestTemplate restTemplate = new RestTemplate(factory);

            List<ClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
            interceptors.add(acceptHeaderInterceptor);
            restTemplate.setInterceptors(interceptors);

            List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

            // 创建并添加 Jackson JSON 消息转换器
            MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
            messageConverters.add(jsonConverter);
            messageConverters.add(new FastJsonHttpMessageConverter());
            messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));

            restTemplate.setMessageConverters(messageConverters);
            return restTemplate;
        }

    }

    public static RestTemplate getInstance() {
        return Build.INSTANCE;
    }

    public static RestTemplate getProxyInstance() {
        return Build.INSTANCE_PROXY;
    }


}
